﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using ClassLibrary.Entity;
using System.Data;

namespace ClassLibrary.DAO
{
    public class TblItemDAO
    {
        private DBService _db = null;

        public TblItemDAO(String connectionString)
        {
            _db = new DBService(connectionString);
        }
        public bool InsUpdItem(TblItem Item, int mode)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("sp_TblItems_InsUp",
                    new string[] { "@ITemID", "@ItemName", "@ItemDescription", "@Price", "@Quantity", "@IDType", "@ItemStatus", "@Mode" },
                    new object[] { Item.ItemID, Item.ItemName, Item.Description, Item.Price, Item.Quantity,  Item.IDType, Item.Status, mode },
                    CommandType.StoredProcedure);
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return flag;
        }

        public DataTable GetItem(int idType)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_TblItems_List",
                    new string[] { "@idType" },
                    new object[] { idType }, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public TblItem GetItemByID(int IDItem)
        {
            TblItem item = new TblItem();
            try
            {
                DataTable dt = _db.ExeSP_DataTable("select vItems.* from vItems " +
                                        " where vItems.ItemID = @IDItem ",
                    new string[] { "@IDItem" },
                    new object[] { IDItem }
                    , CommandType.Text);
                foreach (DataRow dr in dt.Rows)
                {
                    item.ItemID = IDItem;
                    item.ItemName = dr["ItemName"].ToString();                  
                    item.IDType = Convert.ToInt32(dr["IDType"].ToString());
                    item.Quantity = Convert.ToInt32(dr["Quantity"].ToString());
                    item.Price = float.Parse(dr["Price"].ToString());
                    item.Status = Convert.ToInt32(dr["Status"].ToString());
                    item.Description = dr["Description"].ToString();
       
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return item;
        }

        // select image
        public DataTable GetImgItems(int IDItem)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from TblImage where ItemID = @IDItem",
                    new string[] { "@IDItem" },
                    new object[] { IDItem },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }


        //insert imgae
        public bool InsImageItem(TblImage imgItem, int mode)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("sp_TblItemImage_Ins",
                    new string[] { "@p_ItemID", "@p_ImageURL" },
                    new object[] { imgItem.ItemID, imgItem.ImageURL },
                    CommandType.StoredProcedure);
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }
       


    }
}
